package com.ikkong.poor.controller;

import java.util.Map;
import com.ikkong.core.base.BaseController;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.jfinal.ext.shiro.ClearShiro;
import com.ikkong.system.controller.base.UrlPermissController;
import com.ikkong.poor.model.Place;
import com.ikkong.poor.service.PlaceService;
import com.ikkong.poor.service.impl.PlaceServiceImpl;
import com.ikkong.system.meta.intercept.PlaceGroupIntercept;
import com.ikkong.system.meta.intercept.PlaceIntercept;
import org.apache.shiro.authz.annotation.RequiresUser;

/**
 * Generated by Blade.
 * 2020-04-03 22:24:04
 */
public class PlaceController extends UrlPermissController {
	private static String CODE = "place";
	private static String PERFIX = "place";
	private static String LIST_SOURCE = "Place.list";
	private static String BASE_PATH = "/platform/place/";
	
	PlaceService service = new PlaceServiceImpl();
	
	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "place.html");
	}

	public void country() {
		setAttr("code", CODE);
		render(BASE_PATH + "place_country.html");
	}



	public void add() {
		setAttr("code", CODE);
		render(BASE_PATH + "place_add.html");
	}

	@ClearShiro
	@RequiresUser
	public void addCountry() {
		setAttr("code", CODE);
		render(BASE_PATH + "place_add_country.html");
	}

	public void edit() {
		String id = getPara(0);
		Place place = service.findById(id);
		setAttr("model", JsonKit.toJson(place));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "place_edit.html");
	}

	@ClearShiro
	@RequiresUser
	public void editCountry() {
		String id = getPara(0);
		Place place = service.findById(id);
		setAttr("model", JsonKit.toJson(place));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "place_edit_country.html");
	}

	public void view() {
		String id = getPara(0);
		Place place = service.findById(id);
		setAttr("model", JsonKit.toJson(place));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "place_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE,new PlaceGroupIntercept());
		renderJson(grid);
	}

	@ClearShiro
	@RequiresUser
	public void countryList(){
		Object grid = paginate(LIST_SOURCE,new PlaceIntercept());
		renderJson(grid);
	}

	public void save() {
		Place place = mapping(PERFIX, Place.class);
		if(place.getPid() == null){
			place.setPid(1);
		}
		boolean temp = service.save(place);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		Place place = mapping(PERFIX, Place.class);
		boolean temp = service.update(place);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
}
